package com.kxmall.huishou.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kxmall.common.core.page.TableDataInfo;
import com.kxmall.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kxmall.common.utils.StringUtils;
import com.kxmall.user.domain.KxUser;
import com.kxmall.user.domain.bo.KxUserBo;
import com.kxmall.user.domain.vo.KxUserVo;
import com.kxmall.user.mapper.KxUserMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Service;
import com.kxmall.huishou.domain.bo.HsUserSignBo;
import com.kxmall.huishou.domain.vo.HsUserSignVo;
import com.kxmall.huishou.domain.HsUserSign;
import com.kxmall.huishou.mapper.HsUserSignMapper;
import com.kxmall.huishou.service.IHsUserSignService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户签到记录Service业务层处理
 *
 * @author kxmall
 * @date 2023-11-23
 */
@RequiredArgsConstructor
@Service
public class HsUserSignServiceImpl implements IHsUserSignService {

    private final HsUserSignMapper baseMapper;

    private final KxUserMapper kxUserMapper;

    /**
     * 查询用户签到记录
     */
    @Override
    public HsUserSignVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询用户签到记录列表
     */
    @Override
    public TableDataInfo<HsUserSignVo> queryPageList(HsUserSignBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HsUserSign> lqw = buildQueryWrapper(bo);
        Page<HsUserSignVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户签到记录列表
     */
    @Override
    public List<HsUserSignVo> queryList(HsUserSignBo bo) {
        LambdaQueryWrapper<HsUserSign> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HsUserSign> buildQueryWrapper(HsUserSignBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HsUserSign> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, HsUserSign::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getSignDate()), HsUserSign::getSignDate, bo.getSignDate());
        lqw.eq(StringUtils.isNotBlank(bo.getUserName()), HsUserSign::getUserName, bo.getUserName());
        return lqw;
    }

    /**
     * 新增用户签到记录
     */
    @Override
    public Boolean insertByBo(HsUserSignBo bo) {
        HsUserSign add = BeanUtil.toBean(bo, HsUserSign.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        try {
            KxUserVo kxUserVo = kxUserMapper.selectVoById(bo.getUserId());
            if(kxUserVo != null){

                HsUserSignBo hsUserSignBoData = new HsUserSignBo();
                hsUserSignBoData.setUserId(bo.getUserId());
                hsUserSignBoData.setSignDate(DateFormatUtils.format(System.currentTimeMillis() - 3600*1000l*24,"yyyy-MM-dd") );
                LambdaQueryWrapper<HsUserSign> lqw = buildQueryWrapper(hsUserSignBoData);
                List<HsUserSignVo> dataList = baseMapper.selectVoList(lqw);

                long signNum = 1;

                if(dataList != null && dataList.size() >0 ){
                    signNum = kxUserVo.getSignNum() +1l;
                }

                KxUser kxUser = new KxUser();
                kxUser.setUid(kxUserVo.getUid());
                kxUser.setSignNum(signNum);
                kxUserMapper.update(kxUser, new QueryWrapper<KxUser>().eq("uid",kxUserVo.getUid()));

            }

        }catch (Exception e){
            e.printStackTrace();
        }

        return flag;
    }

    /**
     * 修改用户签到记录
     */
    @Override
    public Boolean updateByBo(HsUserSignBo bo) {
        HsUserSign update = BeanUtil.toBean(bo, HsUserSign.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HsUserSign entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户签到记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
